﻿using MusicDiary.Lib.Models;
using MusicPlayer.Data.DataModel;

namespace MusicDiary.Lib.Service;

public static class DataService
{
    public static MusicDataModel ToMusicData(MusicModel model)
    {
        return new MusicDataModel() { Url = model.Url };
    }

    public static PlaylistDataModel ToPlaylistData(PlaylistModel model)
    {
        return new PlaylistDataModel
        {
            Musics = model.Musics.Select(ToMusicData).ToList(),
            Name = model.Name
        };
    }

    public static HistoryDataModel ToHistoryData(MusicModel model)
    {
        return new HistoryDataModel() { Url = model.Url ,LastListenTime = DateTime.Now.ToString("f")};
    }

    public static MusicModel ToMusic(MusicDataModel model)
    {
        return new MusicModel(model.Url);
    }

    public static MusicModel ToMusic(HistoryDataModel model)
    {
        return new MusicModel(model.Url);
    }

    public static PlaylistModel ToPlaylist(PlaylistDataModel model)
    {
        var a = model.Musics.Select(ToMusic);
        var musicModels = a as MusicModel[] ?? a.ToArray();
        string? Image = null;
        if (musicModels.Any())
            Image ??= musicModels[0].Url;
        
        return new PlaylistModel()
        {
            Musics = model.Musics.Select(ToMusic).ToList(), 
            Key = model.Key,
            Name = model.Name,
            Image = Image
        };
    }
    
    
}